<?php
namespace App\Http\Controllers;

class DyController
{
    //测试抖音

    public function sousuo()
    {
        $url = 'https://www.douyin.com/search/%E5%9C%A8%E5%8E%BF%E5%9F%8E%E5%A6%82%E4%BD%95%E9%94%80%E5%94%AE%E5%B0%8F%E7%A8%8B%E5%BA%8F?type=user';
        $opts = [
            'http' => [
                'header' => 'User-Agent: Mozilla/5.0 (Linux ; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Mobile Safari/537.36"',
            ],
        ];
        $cnts = stream_context_create($opts);
        $html = file_get_contents($url, false, $cnts);
        ddd($html);
    }

//    public function sousuo()
//    {
//        $rr = file_get_contents('https://www.douyin.com/search/%E5%9C%A8%E5%8E%BF%E5%9F%8E%E5%A6%82%E4%BD%95%E9%94%80%E5%94%AE%E5%B0%8F%E7%A8%8B%E5%BA%8F?type=user');
//        ddd($rr);
////        $url = 'https://www.douyin.com/search/' . urlencode('火锅店') . '?type=user';
//        $url = 'https://www.douyin.com/search/' . urlencode('在县城如何销售小程序') . '?type=user';
//        $headers = [
//            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
//            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
//            'Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
//            'Accept-Encoding: gzip, deflate',
//            'Connection: keep-alive',
//            'Upgrade-Insecure-Requests: 1',
//        ];
//        $html = $this->curlRequest($url, $headers);
//        echo "搜索结果: \n";
//        return $this->parseUserData($html);
//    }
//
//    private function parseUserData($html)
//    {
//        // 使用正则或DOM解析器提取用户信息
//        preg_match_all('/<div class="user-item">(.*?)<\/div>/s', $html, $matches);
//        $users = [];
//        if (!empty($matches[1])) {
//            foreach ($matches[1] as $userHtml) {
//                $user = [
//                    'name' => $this->extractText($userHtml, '/<div class="name">(.*?)<\/div>/'),
//                    'signature' => $this->extractText($userHtml, '/<div class="signature">(.*?)<\/div>/'),
//                    'fans_count' => $this->extractText($userHtml, '/<div class="fans">(.*?)<\/div>/'),
//                ];
//                $users[] = $user;
//            }
//        }
//        return $users;
//    }
//
//    private function extractText($html, $pattern)
//    {
//        preg_match($pattern, $html, $match);
//        return isset($match[1]) ? trim($match[1]) : '';
//    }
//
//    private function curlRequest($url, $headers = [])
//    {
//        $ch = curl_init();
//        curl_setopt_array($ch, [
//            CURLOPT_URL => $url,
//            CURLOPT_RETURNTRANSFER => true,
//            CURLOPT_SSL_VERIFYPEER => false,
//            CURLOPT_SSL_VERIFYHOST => false,
//            CURLOPT_ENCODING => 'gzip, deflate',
//            CURLOPT_HTTPHEADER => $headers,
//            CURLOPT_CONNECTTIMEOUT => 10,
//            CURLOPT_TIMEOUT => 15,
//            CURLOPT_FOLLOWLOCATION => true,
//            CURLOPT_MAXREDIRS => 5,
//        ]);
//        $response = curl_exec($ch);
//        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
//        if (curl_errno($ch)) {
//            throw new \Exception('CURL错误: ' . curl_error($ch));
//        }
//        curl_close($ch);
//        if ($httpCode !== 200) {
//            throw new \Exception('HTTP请求失败，状态码: ' . $httpCode);
//        }
//        return $response;
//    }
}